//
//  HomeDeviceView.m
//  Lightthings
//
//  Created by Hero on 2024/11/16.
//

#import "HomeDeviceView.h"
#import "MainSectionModel.h"
#import "BECollectionToCollectionModel.h"
#import "KLCollectionBaseLayout.h"
#import <CALayer+YYAdd.h>
#import "BESimpleCollectionViewAdapter.h"
#import "HomeDeviceViewModel.h"
#import "FRIDefine.h"
@interface HomeDeviceView ()
@property (nonatomic, strong) UICollectionView * collectionView;
@property (nonatomic, strong) NSMutableArray * dataArr;
@property (nonatomic, strong) MainSectionModel * s;
@property (strong, nonatomic) NSArray <MainSectionModel *>* __nullable dataSources;

@end

@implementation HomeDeviceView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 10;
        self.layer.shadowColor = [UIColor blackColor].CGColor;
        self.layer.shadowOpacity = 0.2;
        self.layer.shadowOffset = CGSizeZero;
        self.layer.shadowRadius = 10;
        self.layer.masksToBounds = NO;
        
        CAGradientLayer * layer = [[CAGradientLayer alloc] init];
        layer.frame = self.bounds;
        layer.colors = @[(id)KLColor(@"#FFFFFF").CGColor, (id)KLColor(@"#ECFAF4").CGColor ,(id)KLColor(@"#E2F6ED").CGColor];
        layer.startPoint = CGPointMake(0, 0);
        layer.endPoint = CGPointMake(1, 0);
        layer.cornerRadius = 10;
        layer.masksToBounds = YES;
        layer.locations = @[@0, @0.5, @1];
        [self.layer insertSublayer:layer atIndex:0];
        
        notice_add(self, @selector(refreshUI), FRINotificationAtDidConnectPeripheral, nil);
        notice_add(self, @selector(refreshUI), FRINotificationAtDidDisconnectPeripheral, nil);
        
        MainSectionModel * s = [[MainSectionModel alloc] init];
        [s sectionInsets:kEdge(0, 0, 0, 0) ls:10 is:10];
        s.sectionColor = [UIColor clearColor];
        _s = s;
//        s.decorator = ^(UIView * _Nonnull wrapper) {
//            wrapper.backgroundColor = [UIColor clearColor];
//        };
//        for (int i = 1; i <3; i ++) {
//            HomeDeviceViewModel * m = [[HomeDeviceViewModel alloc] init];
//            m.action = kMsgPackForward(@"clickDevice:", WKProxy(self), m);
//            [s.items addObject:m];
//        }
        MainSectionModel * s2 = [[MainSectionModel alloc] init];
        [s2 sectionInsets:kEdge(0, 10, 0, 10) ls:10 is:10];
        s2.sectionColor = [UIColor clearColor];
        HomeDeviceViewModel * addM = [[HomeDeviceViewModel alloc] init];
        addM.action = kRunCMD(@"_show_searchView");
        addM.type = 1000;
        addM.img = @"添加＋";
        addM.name = SWString(@"添加设备");
        [s2.items addObject:addM];
        
        self.dataSources = @[s, s2];
        [self _reloadIfNeeded];
        self.collectionView.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)refreshUI {
    [self.s.items removeAllObjects];
    NSArray< CBPeripheral *> * arr = [FRQBluetoothManager shareManager].curPeripherals;
    for (int i = 0; i < arr.count; i ++) {
        HomeDeviceViewModel * m = [[HomeDeviceViewModel alloc] init];
        m.action = kMsgPackForward(@"clickDevice:", WKProxy(self), m);
        m.uuid = arr[i].identifier.UUIDString;
        m.name = arr[i].name;
        m.type = arr[i].deviceType.intValue;
        [self.s.items addObject:m];
    }
    [self _reloadIfNeeded];
}

- (void)clickDevice:(HomeDeviceViewModel *)model {
    if (model.type == BleDevice_Watch) {
        ZHRunEngine(kRunCMDINFO(@"_show_deviceInfo",@{@"uuid":model.uuid}), nil);
    }else if (model.type == BleDevice_Pillow ){
        ZHRunEngine(kRunCMDINFO(@"show_PillowDeviceInfoView",@{@"uuid":model.uuid}), nil);
    }
}

- (void)_reloadIfNeeded{
    [UIView performWithoutAnimation:^{
        self.collectionView.collectionViewAdapter.keyPathOfSubData = @"items";
        self.collectionView.cellDatas = self.dataSources;
        self.collectionView.headerDatas = self.dataSources;
        self.collectionView.footerDatas = self.dataSources;
        [self.collectionView reloadData];
    }];
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        KLCollectionBaseLayout *layout = [[KLCollectionBaseLayout alloc] init];
        layout.scrollDirection          = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing       = 0;
        layout.minimumInteritemSpacing  = 0;
        UICollectionView *obj = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        obj.showsHorizontalScrollIndicator = NO;
        obj.backgroundColor = UIColor.whiteColor;
        [self addSubview:_collectionView = obj];
        [obj mas_makeConstraints:^(MASConstraintMaker *make) {
            kMakeTV(10);
            kMakeBV(-10);
            kMakeLV(10);
            kMakeRV(-10);
        }];
       
        if (@available(iOS 11.0, *)) {
            obj.contentInsetAdjustmentBehavior     = UIScrollViewContentInsetAdjustmentNever;
        }
        obj.collectionViewAdapter = [BESimpleCollectionViewAdapter new];
//        obj.collectionViewAdapter.controller = self;
    }
    return _collectionView;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
